#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

// 线程1的函数，用于计算并打印1到10的累加和
void *thread1_function(void *arg)
{
    int sum = 0;
    for (int i = 1; i <= 10; i++)
    {
        sum += i;
        printf("%d ", sum);
        fflush(stdout);
        sleep(1);
    }
    printf("\n");
    return NULL;
}

// 线程2的函数，用于打印主线程传递的字符串
void *thread2_function(void *arg)
{
    char *str = (char *)arg;
    printf("Received string: %s\n", str);
    return NULL;
}

int main()
{
    pthread_t thread1, thread2;
    char message[] = "Hello, this is a test string.";

    // 创建线程1
    if (pthread_create(&thread1, NULL, thread1_function, NULL) != 0)
    {
        perror("pthread_create for thread1");
        return 1;
    }

    // 创建线程2
    if (pthread_create(&thread2, NULL, thread2_function, (void *)message) != 0)
    {
        perror("pthread_create for thread2");
        return 1;
    }

    // 等待线程1结束
    if (pthread_join(thread1, NULL) == 0)
    {
        printf("join success for thread1\n");
    }
    else
    {
        printf("join failed for thread1\n");
    }

    // 等待线程2结束
    if (pthread_join(thread2, NULL) == 0)
    {
        printf("join success for thread2\n");
    }
    else
    {
        printf("join failed for thread2\n");
    }

    return 0;
}